# Copyright 2015, Wind River Inc.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE.txt or https://www.bfgroup.xyz/b2/LICENSE.txt)

#
# Diab C++ Compiler
#

import feature generators common ;
import toolset : flags ;
import os ;

feature.extend toolset : diab ;

# Inherit from Unix toolset to get library ordering magic.
toolset.inherit  diab : unix ;

generators.override diab.prebuilt : builtin.lib-generator ;
generators.override diab.prebuilt : builtin.prebuilt ;
generators.override diab.searched-lib-generator : searched-lib-generator ;


rule init ( version ? : command * : options * )
{
    local condition = [ common.check-init-parameters diab : version $(version) ] ;

    local command = [ common.get-invocation-command diab : dcc : $(command) ] ;

    if $(command)
    {
        local root = [ common.get-absolute-tool-path $(command[-1]) ] ;

        if $(root)
        {
            flags diab .root <host-os>linux : "\"$(root)\"/" ;
            flags diab .root <host-os>windows : $(root:T)/ ;
        }
    }
    # If we can't find 'CC' anyway, at least show 'CC' in the commands
    command ?= CC ;

    common.handle-options diab : $(condition) : $(command) : $(options) ;
}

generators.register-c-compiler diab.compile.c++ : CPP : OBJ : <toolset>diab ;
generators.register-c-compiler diab.compile.c : C : OBJ : <toolset>diab ;


# unlike most compilers, Diab defaults to static linking.
# flags cxx LINKFLAGS <runtime-link>static :  ;
flags diab.compile OPTIONS <debug-symbols>on : -g ;
flags diab.link OPTIONS <debug-symbols>on : -g ;

flags diab.compile OPTIONS <optimization>off :  ;
flags diab.compile OPTIONS <optimization>speed : -speed ;
flags diab.compile OPTIONS <optimization>space : -size ;

# flags diab.compile OPTIONS <inlining>off  : -Xinline=0  ;
# flags diab.compile OPTIONS <inlining>on   : -Xinline=10 ;
# flags diab.compile OPTIONS <inlining>full : -Xinline=50 ;

flags diab.compile OPTIONS <cflags> ;
flags diab.compile.c++ OPTIONS <cxxflags> ;
flags diab.compile DEFINES <define> ;

flags diab.compile.c++ OPTIONS <exception-handling>off  : -Xno-exceptions ;
# So Dinkum STL knows when exceptions are disabled
flags diab.compile.c++ DEFINES <exception-handling>off  : _NO_EX=1 ;
flags diab.compile.c++ DEFINES <rtti>off : _NO_RTTI ;
flags diab.compile INCLUDES <include> ;
flags diab.link OPTIONS <linkflags> ;

flags diab.compile OPTIONS <link>shared : -Xpic ;
#flags diab.compile OPTIONS <link>static : ;
# get VxWorks link options from shell environment
flags diab.link OPTIONS <link>static : [ os.environ LDFLAGS_STATIC ]  ;
flags diab.link.dll OPTIONS          : [ os.environ LDFLAGS_SO ] ;
flags diab.link OPTIONS <link>shared : [ os.environ LDFLAGS_DYNAMIC ] ;

flags diab.link LOPTIONS <link>shared : -Xdynamic -Xshared -Xpic ;

flags diab.link LIBPATH <library-path> ;
flags diab.link LIBRARIES <library-file> ;
flags diab.link FINDLIBS-ST <find-static-library> ;
flags diab.link FINDLIBS-SA <find-shared-library> ;

actions link bind LIBRARIES
{
    $(CONFIG_COMMAND) $(OPTIONS) $(LOPTIONS) -o "$(<)" -L$(LIBPATH) "$(>)" "$(LIBRARIES)" -l$(FINDLIBS-ST) -l$(FINDLIBS-SA)
}

# When creating dynamic libraries, we don't want to be warned about unresolved
# symbols, therefore all unresolved symbols are marked as expected by
# '-expect_unresolved *'. This also mirrors the behaviour of the GNU tool
# chain.

actions link.dll bind LIBRARIES
{
    $(.root:E=)dplus $(OPTIONS) $(LOPTIONS) "$(LIBRARIES)" -o "$(<[1])" -L$(LIBPATH)  "$(>)" -l$(FINDLIBS-ST) -l$(FINDLIBS-SA)
}

#rule compile.asm ( targets * : sources * : properties * )
#{
#    setup-fpic $(targets) : $(sources) : $(properties) ;
#    setup-address-model $(targets) : $(sources) : $(properties) ;
#}

actions compile.asm
{
    "$(CONFIG_COMMAND)" $(LANG) $(OPTIONS) -D$(DEFINES) -I"$(INCLUDES)" -c -o "$(<)" "$(>)"
}



actions compile.c
{
    $(.root:E=)dcc -c $(OPTIONS) -D$(DEFINES) -I"$(INCLUDES)" -o "$(<)" "$(>)"
}

actions compile.c++
{
    $(.root:E=)dplus -c $(OPTIONS) -D$(DEFINES) -I"$(INCLUDES)" -o "$(<)" "$(>)"
}

# Always create archive from scratch. See the gcc toolet for rationale.
RM = [ common.rm-command ] ;
actions together piecemeal archive
{
  $(RM) "$(<)"
  dar rc $(<) $(>)
}
